package com.alibaba.csp.sentinel.dashboard.rule.redis.system;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.dashboard.rule.redis.RedisUtil;
import com.alibaba.csp.sentinel.dashboard.rule.redis.RuleConsts;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ZhaoHang
 * @since 2021-08-30 16:17
 */
@Component("systemRuleRedisPublisher")
public class SystemRuleRedisPublisher implements DynamicRulePublisher<List<SystemRuleEntity>> {

    @Autowired
    private RedisUtil redisConfigUtil;

    @Override
    public void publish(String app, List<SystemRuleEntity> rules) throws Exception {
        AssertUtil.notEmpty(app, "app name cannot be empty");
        if (rules == null) {
            return;
        }
        String strs = JSON.toJSONString(rules);
        redisConfigUtil.setString(app + ":" + RuleConsts.RULE_SYSTEM, strs,RuleConsts.RULE_SYSTEM_CHANNEL);
    }
}

